﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AcsCommon;

namespace YiShi.Class
{
    public static class Utility
    {
        public static string GetSelectHtml(DbEnum de, string fieldName)
        {
            return GetSelectHtml(de, fieldName, null);
        }
        public static string GetSelectHtml(DbEnum de, string fieldName, string selectValue)
        {
            return GetSelectHtml(de, fieldName, selectValue, false);
        }
        public static string GetSelectHtml(DbEnum de, string fieldName, string selectValue, bool hasBlank)
        {
            string options = "";
            if (hasBlank)
            {
                string selected = ((selectValue != null) && ("" == selectValue)) ? "selected" : "";
                options += String.Format("<option value='{0}' {1}>{2}</option>", "", selected, "");
            }
            for (int i = 0; i < de.Count; i++)
            {
                EnumItem ei = de[i];
                string selected = ((selectValue != null) && (ei.Value == selectValue)) ? "selected" : "";
                options += String.Format("<option value='{0}' {1}>{2}</option>", ei.Value, selected, ei.Text);
            }
            return "<select name='" + fieldName + "'>" + options + "</select>";
        }
        public static string HtmlEncode(string str)
        {
            string s = "";
            if (str.Length == 0) return "";
            s = str.Replace("&", "&amp;");
            s = s.Replace("<", "&lt;");
            s = s.Replace(">", "&gt;");
            s = s.Replace(" ", "&nbsp;");
            s = s.Replace("'", "&#39;");
            s = s.Replace("\"", "&quot;");
            s = s.Replace("\n", "<br>");
            return s;
        }
        public static string HtmlDecode(string str)
        {
            var s = "";
            if (str.Length == 0) return "";
            s = str.Replace("&amp;", "&");
            s = s.Replace("&lt;", "<");
            s = s.Replace("&gt;", ">");
            s = s.Replace("&nbsp;", " ");
            s = s.Replace("&#39;", "\'");
            s = s.Replace("&quot;", "\"");
            s = s.Replace("<br>", "\n");
            return s;
        }
    }
}
